﻿using System;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using Shrestha.Trigraph;
using System.Windows.Media;
using System.Reflection;

namespace Shrestha.Database
{
    public class MySqlDbManager : DbManager
    {
        private MySqlConnection myConn;
        private const string dateFormatter = "yyyy-MM-dd";

        public List<string> GetTables()
        {
            if (myConn == null) InitConnection();
            MySqlCommand cmd = new MySqlCommand("show Tables;", myConn);
            MySqlDataReader mdr = cmd.ExecuteReader();
            List<string> tableList = new List<string>();
            try
            {
                while (mdr.Read()) tableList.Add(mdr.GetString(0));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                mdr.Close();
            }
            tableList.Sort();
            return tableList;
        }

        public List<string> GetRows()
        {
            if (myConn == null) InitConnection();
            MySqlCommand cmd = new MySqlCommand("describe "+TriGraphResources.tableName+";", myConn);
            MySqlDataReader mdr = cmd.ExecuteReader();
            List<string> rowList = new List<string>();
            try
            {
                while (mdr.Read()) rowList.Add(mdr.GetString(0));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                mdr.Close();
            }
            rowList.Sort();
            return rowList;
        }

        public Dictionary<string, Brush> GetGroupingEvent()
        {
            if (myConn == null) InitConnection();
            int uniqueTypes = int.Parse(ExecuteQuery("select count(distinct "+TriGraphResources.groupName + ") from "+TriGraphResources.tableName).ToString());
            if (uniqueTypes > TriGraphResources.GROUP_NUMBER) return null;

            MySqlCommand cmd = new MySqlCommand("select distinct " + TriGraphResources.groupName + " from "+TriGraphResources.tableName+";", myConn);
            MySqlDataReader mdr = cmd.ExecuteReader();

            Dictionary<string, Brush> dl = new Dictionary<string, Brush>();

            Random rnd = new Random();
            Type brushesType = typeof(Brushes);
            PropertyInfo[] properties = brushesType.GetProperties();
            
            try
            {
                while (mdr.Read()){
                    Brush result = Brushes.Transparent;
                    result = (Brush)properties[rnd.Next(properties.Length)].GetValue(null, null);
                    dl.Add(mdr.GetString(0), result);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                mdr.Close();
            }
            return dl;
        }
        
        public bool InitDb(string host, string database, string user, string password)
        {
            
            TriGraphResources.host = host;
            TriGraphResources.database = database;
            TriGraphResources.user = user;
            TriGraphResources.password = password;

            return InitConnection();
        }

        private bool InitConnection()
        {
            //string strProvider = "Data Source=" + TriGraphResources.host + ";Database=" + TriGraphResources.database + ";User ID=" + TriGraphResources.user + ";Password=" + TriGraphResources.password;
            string strProvider = "Data Source=192.168.0.11;Database=eurovis;User ID=ayush;Password=antimatter";
            try
            {
                myConn = new MySqlConnection(strProvider);
                myConn.Open();
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }

        public List<TriEvent> GetGroupedTriEvents(string queryText)
        {
            InitConnection();
            List<TriEvent> teList = new List<TriEvent>();
            MySqlCommand cmd = new MySqlCommand(queryText, myConn);
            MySqlDataReader mdr = cmd.ExecuteReader();
            while (mdr.Read())
            {
                try
                {
                    TriEvent te = new TriEvent(Double.Parse(mdr.GetDouble(0).ToString()), Double.Parse(mdr.GetDouble(1).ToString()), DateTime.Parse(mdr.GetDateTime(2).ToString()), mdr.GetString(3));
                    teList.Add(te);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            return teList;
        }

        public List<TriEvent> GetTriEvents(string queryText)
        {
            InitConnection();
            List<TriEvent> teList = new List<TriEvent>();
            MySqlCommand cmd = new MySqlCommand(queryText, myConn);
            MySqlDataReader mdr = cmd.ExecuteReader();
            while (mdr.Read())
            {
                try
                {
                    TriEvent te = new TriEvent(Double.Parse(mdr.GetDouble(0).ToString()), Double.Parse(mdr.GetDouble(1).ToString()), DateTime.Parse(mdr.GetDateTime(2).ToString()));
                    teList.Add(te);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            return teList;
        }

        public List<TriClusterEvent> GetClusteredTriEvents(string queryText)
        {

            InitConnection();
            List<TriClusterEvent> teList = new List<TriClusterEvent>();
            MySqlCommand cmd = new MySqlCommand(queryText, myConn);
            MySqlDataReader mdr = cmd.ExecuteReader();
            while (mdr.Read())
            {
                try
                {
                    TriClusterEvent te = new TriClusterEvent(Double.Parse(mdr.GetDouble(0).ToString()), 
                        Double.Parse(mdr.GetDouble(1).ToString()),
                        Double.Parse(mdr.GetDouble(2).ToString()),
                        Double.Parse(mdr.GetDouble(3).ToString()), 
                        DateTime.Parse(mdr.GetDateTime(4).ToString()),
                        DateTime.Parse(mdr.GetDateTime(5).ToString()),
                        int.Parse(mdr.GetString(6)),
                        int.Parse(mdr.GetString(7)));
                    teList.Add(te);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            return teList;
        }

        public object ExecuteQuery(string queryText)
        {
            InitConnection();
            MySqlCommand cmd = new MySqlCommand(queryText, myConn);
            return cmd.ExecuteScalar();
        }

        public WorkingSet ExtractParameters(string tableName, string latName, string lngName, string timeName)
        {
            WorkingSet workingSet = new WorkingSet();
            try
            {
                workingSet.MaxDbLat = Double.Parse(ExecuteQuery("select max(" + latName + ") from " + tableName).ToString());
                workingSet.MinDbLat = Double.Parse(ExecuteQuery("select min(if(" + latName + "=0,NULL," + latName + ")) from " + tableName).ToString());

                workingSet.MaxDbLng = Double.Parse(ExecuteQuery("select max(" + lngName + ") from " + tableName).ToString());
                workingSet.MinDbLng = Double.Parse(ExecuteQuery("select min(if(" + lngName + "=0,NULL," + lngName + ")) from " + tableName).ToString());

                workingSet.MaxDbDate = DateTime.Parse(ExecuteQuery("select max(" + timeName + ") from " + tableName).ToString());
                workingSet.MinDbDate = DateTime.Parse(ExecuteQuery("select min(if(" + timeName + "=0,NULL," + timeName + ")) from " + tableName).ToString());

                workingSet.TotalRows = int.Parse(ExecuteQuery("select count(*) from" + tableName).ToString());

                workingSet.TriEventList = GetTriEvents("select " + latName + "," + lngName + "," + timeName + " from " + tableName + " where " + latName + " IS NOT NULL AND " + lngName + " IS NOT NULL AND " + timeName + " IS NOT NULL");
                return workingSet;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return null;
        }

        public WorkingSet ExtractParameters(string tableName, string latName, string lngName, string timeName, string groupName)
        {
            WorkingSet workingSet = new WorkingSet();
            try
            {
                workingSet.MaxDbLat = Double.Parse(ExecuteQuery("select max(" + latName + ") from " + tableName).ToString());
                workingSet.MinDbLat = Double.Parse(ExecuteQuery("select min(if(" + latName + "=0,NULL," + latName + ")) from " + tableName).ToString());

                workingSet.MaxDbLng = Double.Parse(ExecuteQuery("select max(" + lngName + ") from " + tableName).ToString());
                workingSet.MinDbLng = Double.Parse(ExecuteQuery("select min(if(" + lngName + "=0,NULL," + lngName + ")) from " + tableName).ToString());

                workingSet.MaxDbDate = DateTime.Parse(ExecuteQuery("select max(" + timeName + ") from " + tableName).ToString());
                workingSet.MinDbDate = DateTime.Parse(ExecuteQuery("select min(if(" + timeName + "=0,NULL," + timeName + ")) from " + tableName).ToString());

                workingSet.TotalRows = int.Parse(ExecuteQuery("select count(*) from" + tableName).ToString());

                workingSet.TriEventList = GetGroupedTriEvents("select " + latName + "," + lngName + "," + timeName + ","+groupName+" from " + tableName + " where " + latName + " IS NOT NULL AND " + lngName + " IS NOT NULL AND " + timeName + " IS NOT NULL AND " + groupName + " IS NOT NULL LIMIT 1000");
                return workingSet;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return null;
        }

        public WorkingSet ExtractParameters()
        {
            string tableName = TriGraphResources.tableName;
            string latName = TriGraphResources.latName;
            string lngName = TriGraphResources.lngName;
            string timeName = TriGraphResources.timeName;

            WorkingSet workingSet = new WorkingSet();
            workingSet.IsClustered = false;
            try
            {
                workingSet.MaxDbLat = Double.Parse(ExecuteQuery("select max(" + latName + ") from " + tableName).ToString());
                workingSet.MinDbLat = Double.Parse(ExecuteQuery("select min(if(" + latName + "=0,NULL," + latName + ")) from " + tableName).ToString());

                workingSet.MaxDbLng = Double.Parse(ExecuteQuery("select max(" + lngName + ") from " + tableName).ToString());
                workingSet.MinDbLng = Double.Parse(ExecuteQuery("select min(if(" + lngName + "=0,NULL," + lngName + ")) from " + tableName).ToString());

                workingSet.MaxDbDate = DateTime.Parse(ExecuteQuery("select max(" + timeName + ") from " + tableName).ToString());
                workingSet.MinDbDate = DateTime.Parse(ExecuteQuery("select min(if(" + timeName + "=0,NULL," + timeName + ")) from " + tableName).ToString());

                workingSet.TotalRows = int.Parse(ExecuteQuery("select count(*) from" + tableName).ToString());

                workingSet.TriEventList = GetTriEvents("select " + latName + "," + lngName + "," + timeName + " from " + tableName + " where " + latName + " IS NOT NULL AND " + lngName + " IS NOT NULL AND " + timeName + " IS NOT NULL");
                return workingSet;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return null;
        }

        public WorkingSet ExtractParameters(string condition)
        {
            if (null == condition) return ExtractParameters();

            string tableName = TriGraphResources.tableName;
            string latName = TriGraphResources.latName;
            string lngName = TriGraphResources.lngName;
            string timeName = TriGraphResources.timeName;

            WorkingSet workingSet = new WorkingSet();
            try
            {
                workingSet.MaxDbLat = Double.Parse(ExecuteQuery("select max(" + latName + ") from " + tableName + " where " + condition).ToString());
                workingSet.MinDbLat = Double.Parse(ExecuteQuery("select min(if(" + latName + "=0,NULL," + latName + ")) from " + tableName + " where " + condition).ToString());

                workingSet.MaxDbLng = Double.Parse(ExecuteQuery("select max(" + lngName + ") from " + tableName + " where " + condition).ToString());
                workingSet.MinDbLng = Double.Parse(ExecuteQuery("select min(if(" + lngName + "=0,NULL," + lngName + ")) from " + tableName + " where " + condition).ToString());

                workingSet.MaxDbDate = DateTime.Parse(ExecuteQuery("select max(" + timeName + ") from " + tableName + " where " + condition).ToString());
                workingSet.MinDbDate = DateTime.Parse(ExecuteQuery("select min(if(" + timeName + "=0,NULL," + timeName + ")) from " + tableName + " where " + condition).ToString());

                workingSet.TotalRows = int.Parse(ExecuteQuery("select count(*) from " + tableName + " where " + condition).ToString());

                workingSet.TriEventList = GetTriEvents("select " + latName + "," + lngName + "," + timeName + " from " + tableName + " where " + latName + " IS NOT NULL AND " + lngName + " IS NOT NULL AND " + timeName + " IS NOT NULL AND "+condition);
                return workingSet;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return null;
        }

        public bool CheckIfClustersExist()
        {
            InitConnection();
            string query_a = "select count(*) from information_schema.tables where table_name='" + TriGraphResources.tableName + "_cluster'";
            string query_b = "select count(*) from information_schema.tables where table_name='" + TriGraphResources.tableName + "_ac'";
            int a = int.Parse(ExecuteQuery(query_a).ToString());
            int b = int.Parse(ExecuteQuery(query_b).ToString());
            if(a==0 || b==0 ) return false;
            else if(int.Parse(a.ToString()) == 0 || int.Parse(b.ToString()) == 0) return false;
            else return true;
        }

        public WorkingSet ExtractClusterParameters(int zoomLvl)
        {
            string tableName = TriGraphResources.tableName;
            string latName = TriGraphResources.clusterLatName;
            string lngName = TriGraphResources.clusterLngName;
            string timeName = TriGraphResources.clusterDateName;

            WorkingSet workingSet = new WorkingSet();
            workingSet.IsClustered = true;
            try
            {
                workingSet.MaxDbLat = Double.Parse(ExecuteQuery("select max(max" + latName + ") from " + tableName + "_ac where zoomlvl=" + zoomLvl).ToString());
                workingSet.MinDbLat = Double.Parse(ExecuteQuery("select min(if(min" + latName + "=0,NULL,min" + latName + ")) from " + tableName + "_ac where zoomlvl=" + zoomLvl).ToString());

                workingSet.MaxDbLng = Double.Parse(ExecuteQuery("select max(max" + lngName + ") from " + tableName + "_ac where zoomlvl=" + zoomLvl).ToString());
                workingSet.MinDbLng = Double.Parse(ExecuteQuery("select min(if(min" + lngName + "=0,NULL,min" + lngName + ")) from " + tableName + "_ac where zoomlvl=" + zoomLvl).ToString());

                workingSet.MaxDbDate = DateTime.Parse(ExecuteQuery("select max(max" + timeName + ") from " + tableName + "_ac where zoomlvl=" + zoomLvl).ToString());
                workingSet.MinDbDate = DateTime.Parse(ExecuteQuery("select min(if(min" + timeName + "=0,NULL,min" + timeName + ")) from " + tableName + "_ac where zoomlvl=" + zoomLvl).ToString());

                workingSet.TotalRows = int.Parse(ExecuteQuery("select count(*) from " + tableName + "_ac where zoomlvl= " + zoomLvl).ToString());

                //workingSet.TriEventList = GetTriEvents("select " + latName + "," + lngName + "," + timeName + " from " + tableName + "_cluster where zoomlvl="+zoomLvl);
                workingSet.TriClusterEventList = GetClusteredTriEvents("select maxlat,minlat,maxlng,minlng,maxdate,mindate,cluster,zoomlvl from " + tableName + "_ac where zoomlvl=" + zoomLvl);
                return workingSet;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return null;
        }

        public WorkingSet ExtractClusterParameters(double minLat, double maxLat, double minLng, double maxLng, DateTime startDate, DateTime endDate)
        {
            string tableName = TriGraphResources.tableName;
            string latName = TriGraphResources.clusterLatName;
            string lngName = TriGraphResources.clusterLngName;
            string timeName = TriGraphResources.clusterDateName;
            int zoomLvl = GetZoomLvlFrom(minLat, maxLat, minLng, maxLng, startDate, endDate);
            string condition = "minLat >" + minLat + " && maxLat <" + maxLat + " && minLng >" + minLng + " && maxLng<" + maxLng + " && minDate>" + startDate + " && maxDate<" + endDate;
            return ExtractWithCondition(condition, zoomLvl);
        }


        private WorkingSet ExtractWithCondition(string condition, int zoomLvl)
        {
            string tableName = TriGraphResources.tableName;
            string latName = TriGraphResources.clusterLatName;
            string lngName = TriGraphResources.clusterLngName;
            string timeName = TriGraphResources.clusterDateName;
            WorkingSet workingSet = new WorkingSet();
            workingSet.IsClustered = true;
            try
            {
                workingSet.MaxDbLat = Double.Parse(ExecuteQuery("select max(max" + latName + ") from " + tableName + "_ac where zoomlvl=" + zoomLvl + " and " + condition).ToString());
                workingSet.MinDbLat = Double.Parse(ExecuteQuery("select min(if(min" + latName + "=0,NULL,min" + latName + ")) from " + tableName + "_ac where zoomlvl=" + zoomLvl + " and " + condition).ToString());

                workingSet.MaxDbLng = Double.Parse(ExecuteQuery("select max(max" + lngName + ") from " + tableName + "_ac where zoomlvl=" + zoomLvl + " and " + condition).ToString());
                workingSet.MinDbLng = Double.Parse(ExecuteQuery("select min(if(min" + lngName + "=0,NULL,min" + lngName + ")) from " + tableName + "_ac where zoomlvl=" + zoomLvl + " and " + condition).ToString());

                workingSet.MaxDbDate = DateTime.Parse(ExecuteQuery("select max(max" + timeName + ") from " + tableName + "_ac where zoomlvl=" + zoomLvl + " and " + condition).ToString());
                workingSet.MinDbDate = DateTime.Parse(ExecuteQuery("select min(if(min" + timeName + "=0,NULL,min" + timeName + ")) from " + tableName + "_ac where zoomlvl=" + zoomLvl + " and " + condition).ToString());

                workingSet.TotalRows = int.Parse(ExecuteQuery("select count(*) from " + tableName + "_ac where zoomlvl= " + zoomLvl + " and " + condition).ToString());

                //workingSet.TriEventList = GetTriEvents("select " + latName + "," + lngName + "," + timeName + " from " + tableName + "_cluster where zoomlvl="+zoomLvl);
                workingSet.TriClusterEventList = GetClusteredTriEvents("select maxlat,minlat,maxlng,minlng,maxdate,mindate,cluster,zoomlvl from " + tableName + "_ac where zoomlvl=" + zoomLvl + " and " + condition);
                return workingSet;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return null;
        }

        private int GetZoomLvlFrom(double minLat, double maxLat, double minLng, double maxLng, DateTime startDate, DateTime endDate)
        {
            string tableName = TriGraphResources.tableName;
            string latName = TriGraphResources.clusterLatName;
            string lngName = TriGraphResources.clusterLngName;
            string timeName = TriGraphResources.clusterDateName;

            return int.Parse(ExecuteQuery("select max(P.zl) from (select count(*) as count, zoomlvl as zl from " + tableName + "_ac where min" + timeName + " > " + startDate + " and max" + timeName + "<" + endDate + " group by zoomlvl) P where P.count > " + TriGraphResources.ClusterSearchTune).ToString());
     
        }

        private int GetZoomLvlFrom(string query, string cquery)
        {
            string tableName = TriGraphResources.tableName;
            string latName = TriGraphResources.clusterLatName;
            string lngName = TriGraphResources.clusterLngName;
            string timeName = TriGraphResources.clusterDateName;

            string queryResult = ExecuteQuery("select count(*) from " + tableName + " where " + query).ToString();
            if (int.Parse(queryResult) < 500)
            {
                return 0;
            }
            else
            {
                queryResult = ExecuteQuery("select max(P.zl) from (select count(*) as count, zoomlvl as zl from " + tableName + "_ac where " + cquery + " group by zoomlvl) P where P.count > " + TriGraphResources.ClusterSearchTune).ToString();
                if (null == queryResult || queryResult == "") return 0;
                else
                    return int.Parse(queryResult);
            }
        }




        public WorkingSet ExtractParameters(string query, string cquery, bool isClustered)
        {
            if (!isClustered)
            {
                return ExtractParameters(query);
            }
            else
            {
                int zoomLvl = GetZoomLvlFrom(query, cquery);
                zoomLvl = zoomLvl < 5 ? 0 : zoomLvl;
                if (zoomLvl == 0)
                    return ExtractParameters(query);
                else
                    return ExtractWithCondition(cquery, zoomLvl);
            }

        }

        public WorkingSet ExtractParameters(TriClusterEvent tce)
        {
            string condition = TriGraphResources.latName + " <=" + tce.MaxLat + " and " +
                    TriGraphResources.latName + " >=" + tce.MinLat + " and " +
                    TriGraphResources.lngName + " <=" + tce.MaxLng + " and " +
                    TriGraphResources.lngName + " >=" + tce.MinLng + " and " +
                    TriGraphResources.timeName + " <='" + tce.MaxDate.ToString(dateFormatter) + "' and " +
                    TriGraphResources.timeName + " >='" + tce.MinDate.ToString(dateFormatter) + "'";
            string query = "select count(*) from " + TriGraphResources.tableName + " where " + condition;

            int count = int.Parse(ExecuteQuery(query).ToString());

            if (count < 25)
            {
                WorkingSet ws = ExtractParameters(condition);
                ws.IsClustered = false;
                return ws;
            }
            else
            {
                WorkingSet workingSet = new WorkingSet();
                query = "select w.max" + TriGraphResources.clusterLatName + ", w.min" + TriGraphResources.clusterLatName +
                   ", w.max" + TriGraphResources.clusterLngName + ", w.min" + TriGraphResources.clusterLngName +
                   ", w.max" + TriGraphResources.clusterDateName + ", w.min" + TriGraphResources.clusterDateName +
                   ", w.cluster, w.zoomlvl " +
                   " from " + TriGraphResources.tableName + "_ac w, " +
               "(select max(zoomlvl) zl from " + TriGraphResources.tableName + "_ac where zoomlvl<" + tce.ZoomLvl + ") z, " +
               "(select max" + TriGraphResources.clusterLatName + ", min" + TriGraphResources.clusterLatName +
               ",max" + TriGraphResources.clusterLngName + ",min" + TriGraphResources.clusterLngName +
               ",max" + TriGraphResources.clusterDateName + ",min" + TriGraphResources.clusterDateName +
               " from " + TriGraphResources.tableName + "_ac where zoomlvl=" + tce.ZoomLvl + " and cluster=" + tce.ClusterId + ") x where" +
                   " w.max" + TriGraphResources.clusterLatName + "<= x.max" + TriGraphResources.clusterLatName +
                   " AND w.min" + TriGraphResources.clusterLatName + ">= x.min" + TriGraphResources.clusterLatName +
                   " AND w.max" + TriGraphResources.clusterLngName + "<= x.max" + TriGraphResources.clusterLngName +
                   " AND w.min" + TriGraphResources.clusterLngName + ">= x.min" + TriGraphResources.clusterLngName +
                   " AND w.max" + TriGraphResources.clusterDateName + "<= x.max" + TriGraphResources.clusterDateName +
                   " AND w.min" + TriGraphResources.clusterDateName + ">= x.min" + TriGraphResources.clusterDateName +
                   " AND w.zoomlvl=z.zl";


                List<TriClusterEvent> tces = GetClusteredTriEvents(query);

                workingSet.IsClustered = true;
                workingSet.TriClusterEventList = tces;

                workingSet.MaxDbLat = Double.Parse(ExecuteQuery("select max(max" + TriGraphResources.clusterLatName + ") from " + TriGraphResources.tableName + "_ac where zoomlvl=" + tce.ZoomLvl + " and cluster=" + tce.ClusterId).ToString());
                workingSet.MinDbLat = Double.Parse(ExecuteQuery("select min(if(min" + TriGraphResources.clusterLatName + "=0,NULL,min" + TriGraphResources.clusterLatName + ")) from " + TriGraphResources.tableName + "_ac where zoomlvl=" + tce.ZoomLvl + " and cluster=" + tce.ClusterId).ToString());

                workingSet.MaxDbLng = Double.Parse(ExecuteQuery("select max(max" + TriGraphResources.clusterLngName + ") from " + TriGraphResources.tableName + "_ac where zoomlvl=" + tce.ZoomLvl + " and cluster=" + tce.ClusterId).ToString());
                workingSet.MinDbLng = Double.Parse(ExecuteQuery("select min(if(min" + TriGraphResources.clusterLngName + "=0,NULL,min" + TriGraphResources.clusterLngName + ")) from " + TriGraphResources.tableName + "_ac where zoomlvl=" + tce.ZoomLvl + " and cluster=" + tce.ClusterId).ToString());

                workingSet.MaxDbDate = DateTime.Parse(ExecuteQuery("select max(max" + TriGraphResources.clusterDateName + ") from " + TriGraphResources.tableName + "_ac where zoomlvl=" + tce.ZoomLvl + " and cluster=" + tce.ClusterId).ToString());
                workingSet.MinDbDate = DateTime.Parse(ExecuteQuery("select min(if(min" + TriGraphResources.clusterDateName + "=0,NULL,min" + TriGraphResources.clusterDateName + ")) from " + TriGraphResources.tableName + "_ac where zoomlvl=" + tce.ZoomLvl + " and cluster=" + tce.ClusterId).ToString());

                workingSet.TotalRows = count;
                return workingSet;
            }
        }


        
    }
}
